package com.example.servicea.utils;

import com.xiaoleilu.hutool.codec.Base64;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * jwt工具类
 * @author hutu
 * @date 2018/4/2 16:41
 */
@ConfigurationProperties(prefix = "greentree.jwt")
@Component
public class JwtUtils {
    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * des加密key
     */
    private final static String DES_SECRET_KEY = "bWluZyBodWEgcWlhbmcgTE9WRSB4dSB0YWkgbGlhbiAh";
    private String secret;
    private long expire;
    private String header;

    /**
     * 生成jwt token
     */
    public String generateToken(String subject) {
        Date nowDate = new Date();
        //过期时间
        Date expireDate = new Date(nowDate.getTime() + expire * 1000);
        String token = Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setSubject(subject)
                .setIssuedAt(nowDate)
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
        System.out.println("试试"+token);
        String ss = Base64.decodeStr(token);
        System.out.println(Base64.decodeStr(token));
        return token;
    }

    public static void main(String[] args) {
        String s = Base64.decodeStr(DES_SECRET_KEY);
        System.out.println(s);
        new JwtUtils().generateToken("123456");
    }

    /**
     * 解析jwt token
     */
    public Claims getClaimByToken(String token) {
        try {
            return Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            logger.debug("validate is token error ", e);
            return null;
        }
    }

    /**
     * des 对称加密
     *
     * @param param
     * @return
     */
    public static String encode(String param) {
        return Base64.encode(SecureUtil.des(DES_SECRET_KEY.getBytes()).encrypt(param));
    }

    /**
     * des 对称解密
     *
     * @param param
     * @return
     */
    public static String decode(String param) {
        byte[] after = SecureUtil.des(DES_SECRET_KEY.getBytes()).decrypt(Base64.decode(param));
        return new String(after);
    }

    /**
     * token是否过期
     *
     * @return true：过期
     */
    public boolean isTokenExpired(Date expiration) {
        return expiration.before(new Date());
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public long getExpire() {
        return expire;
    }

    public void setExpire(long expire) {
        this.expire = expire;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }
}
